package gemeente.meldStad;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("unchecked")
public class MainItimizedOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private boolean clickable = true;
	private boolean isPinch = false;
	
	public MainItimizedOverlay(Drawable defaultMarker, Context context) {
		//boundcenterBottom() -> midden-onder van marker als locatie 
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}
	
	public MainItimizedOverlay(Drawable defaultMarker, Context context, boolean click) {
		//boundcenterBottom() -> midden-onder van marker als locatie 
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		clickable = click;
	}

	public void addOverlay(OverlayItem overlay) {
	    //add item to item-arraylist
		mOverlays.add(overlay);
	    
	    //populate calls createItem
	    populate();
	}
	
	public void clear(){
		mOverlays.clear();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
		if(isPinch){
			return false;
		}else if(clickable == true){
		  OverlayItem item = mOverlays.get(index);
		  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		  dialog.setTitle(item.getTitle());
		  dialog.setMessage(item.getSnippet());
		  dialog.show();
		}
	  return true;
	}
	
	public boolean onTouchEvent(MotionEvent e, MapView mapView)
	{
	    int fingers = e.getPointerCount();
	    if ( e.getAction()==0 ) isPinch=false;                  // touch down, don't know it's a pinch yet
	    if ( e.getAction()==2 && fingers==2 ) isPinch=true;     // Two fingers, def a pinch
	    return super.onTouchEvent(e,mapView);
	}
}
